package com.Telien.app.ui.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.request.service.RequestService;
import com.Telien.app.user.object.User;

public class CreateRide extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	private User user;
	String[] options = createRange(1, 100);
	String[] timeOptions = {"12:00", "12:15", "12:30", "12:45", "1:00", "1:15", "1:30", "1:45", "2:00", "2:15", "2:30", "2:45", "3:00",
			"3:15", "3:30", "3:45", "4:00", "4:15", "4:30", "4:45", "5:00", "5:15", "5:30", "5:45", "6:00", "6:15", "6:30", "6:45", "7:00",
			"7:15", "7:30", "7:45", "8:00", "8:15", "8:30", "8:45", "9:00", "9:15", "9:30", "9:45", "10:00", "10:15", "10:30", "10:45", "11:00",
			"11:15", "11:30", "11:45"};
	String[] timeOptionsHours = createRange(1, 12);
	String[] timeOptionsMinutes = createRange(0, 59);
	String[] timeAMPM = {"AM", "PM"};
	String[] months = createRange(1, 12);
	String[] days = createRange(1, 31);
	String[] year = createRange(2015, 2020);
	
	JTextField kmlField = new JTextField();
	JLabel lblFile = new JLabel("KML File:");
	JLabel lblSeats = new JLabel("Seats Available:");
	JLabel lblDepartureTime = new JLabel("Departure Time:");
	JLabel lblStartRadius = new JLabel ("Start Radius:");
	JLabel lblEndRadius = new JLabel ("End Radius:");
	JLabel lblAMPM = new JLabel ("AM/PM:");
	JComboBox seatsField = new JComboBox(options);
	JComboBox DepartureTimeField = new JComboBox(timeOptions);
	JComboBox AMPMField = new JComboBox(timeAMPM);
	JComboBox startBox = new JComboBox(options);
	JComboBox endBox = new JComboBox(options);
	JButton create_btn = new JButton("Create");
	JButton back_btn = new JButton("Cancel");	
	JButton file_btn = new JButton("Open KML File");
	
	JLabel lblDay = new JLabel("Day");
	JLabel lblMonth = new JLabel("Month");
	JLabel lblYear = new JLabel("Year");
	JComboBox monthBox = new JComboBox(months);
	JComboBox yearBox = new JComboBox(year);
	JComboBox dayBox = new JComboBox(days);
	JComboBox hourBox = new JComboBox(timeOptionsHours);
	JComboBox minuteBox = new JComboBox(timeOptionsMinutes);
	
	private RequestService requestService;
	
	public CreateRide(User usr) {
		user = usr;
		this.initialize(); 
		requestService = new RequestService();
	}
	
	private String[] createRange(int bottom, int top) {
		ArrayList<String> range = new ArrayList<String>();
		for (int x = bottom;  x <= top; x++) {
			range.add(x + "");
		}
		return range.toArray(new String[range.size()]);
	}
	
	private void initialize() {		
		setSize(600, 500);
		setLayout(null);
				
		lblFile.setBounds(12, 55, 95, 16);
		add(lblFile);
		
		lblSeats.setBounds(12, 100, 95, 16);
		add(lblSeats);
	
		lblDepartureTime.setBounds(12, 145, 95, 16);
		add(lblDepartureTime);
				
		kmlField.setBounds(114, 50, 200, 25);
		add(kmlField);
		kmlField.setColumns(10);
		
		seatsField.setBounds(114, 95, 200, 27);
		add(seatsField);
		
		//DepartureTimeField.setBounds(114, 140, 200, 27);
		//add(DepartureTimeField);
		
		hourBox.setBounds(114, 140, 100, 27);
		add(hourBox);
		
		minuteBox.setBounds(214, 140, 100, 27);
		add(minuteBox);
		
		lblStartRadius.setBounds(340, 56, 314, 25);
		add(lblStartRadius);
		
		lblEndRadius.setBounds(340, 76, 314, 75);
		add(lblEndRadius);
		
		lblAMPM.setBounds(340, 106, 314, 100);
		add(lblAMPM);
		
		startBox.setBounds(440, 56, 45, 25);
		add(startBox);
		
		endBox.setBounds(440, 100, 45, 25);
		add(endBox);
		
		AMPMField.setBounds(440, 144, 45, 25);
		add(AMPMField);
		
		create_btn.addActionListener(this);
		create_btn.setBounds(38, 250, 97, 25);
		add(create_btn);
				
		back_btn.addActionListener(this);
		back_btn.setBounds(147, 250, 97, 25);
		add(back_btn);
		
		file_btn.addActionListener(this);
		file_btn.setBounds(256, 250, 115, 25);
		add(file_btn);
		
		lblDay.setBounds(12, 175, 80, 25);
		add(lblDay);
		
		lblMonth.setBounds(100, 175, 80, 25);
		add(lblMonth);
		
		lblYear.setBounds(188, 175, 80, 25);
		add(lblYear);
		
		dayBox.setBounds(12, 200, 80, 25);
		add(dayBox);
		
		monthBox.setBounds(100, 200, 80, 25);
		add(monthBox);
		
		yearBox.setBounds(188, 200, 80, 25);
		add(yearBox);
					
		
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String name = e.getActionCommand();
		if (name.equals("Create")) {
			create();
		}
		else if (name.equals("Cancel")) {
			clearFields();
		}
		else if (name.equals("Open KML File")) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.showOpenDialog(null);
			File file = fileChooser.getSelectedFile();
			String path = "";
			if (file != null)
				path = file.getPath();
			kmlField.setText(path);
		}			
	}
	
	private void create() {
		System.out.println("Create");
		//TODO
		try {
			String kmlPath = kmlField.getText();
			String seatsAvailable = (String)seatsField.getSelectedItem();
			String departureTime = (String)hourBox.getSelectedItem() + ":" + (String)minuteBox.getSelectedItem() + " " + (String)AMPMField.getSelectedItem();
			String startRadius = (String)startBox.getSelectedItem();
			String endRadius = (String)endBox.getSelectedItem();
			String day = (String)dayBox.getSelectedItem();
			String month = (String)monthBox.getSelectedItem();
			String year = (String)yearBox.getSelectedItem();
			String date = day + "/" + month + "/" + year + " " + departureTime;
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
			Date testDate = new Date();
			testDate = dateFormat.parse(date); //GOT YOUR DATE RIGHT HERE
			DriverRequest request = new DriverRequest(user.getUserUUID(),kmlPath,Integer.parseInt(startRadius),
					Integer.parseInt(endRadius),Integer.parseInt(seatsAvailable),
					testDate,testDate);
			//request.setId();
			requestService.addRequest(request);
		} 
		catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void clearFields(){
		kmlField.setText("");
		seatsField = new JComboBox(options);
		DepartureTimeField = new JComboBox(timeOptions);
		AMPMField = new JComboBox(timeAMPM);
		startBox = new JComboBox(options);
		endBox = new JComboBox(options);
	}
}
